#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5,M=1e6+5;
#define P pair<int,int>
#define px first
#define py second
int n,T,a[N],pre[N],t[M];
ll f[N],mx_pr[N];
inline int read(){
    int x=0,flg=1;
    char ch;
    while(!isdigit(ch=getchar())) if(ch=='-') flg=-1;
    while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^'0'),ch=getchar();
    return x*flg;
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    T=read();
    while(T--){
        ll ans=0;
        n=read();
        int m=0;
        for(int i=1,x;i<=n;i++){
            x=read();
            if(x!=a[m]) a[++m]=x;
            else ans+=x;
        }
        memset(t,0,sizeof t);
        memset(f,0,sizeof f);
        for(int i=1;i<=m;i++) pre[i]=t[a[i]],t[a[i]]=i;
        for(int i=1;i<=m;i++){
            if(pre[i]){
                f[i]=mx_pr[pre[i]+1]+a[i];
                //if(a[pre[i]-1]==a[pre[i]+1]) f[i]=max(f[i],mx_pr[pre[i-1]]+a[pre[i]-1]+a[i]);
            }
            //cerr<<i<<":"<<f[i]<<endl;
            mx_pr[i]=max(mx_pr[i-1],f[i]);
        }
        printf("%lld\n",ans+mx_pr[m]);
    }

    return 0;
}
